package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: qingtao
 * @Date 2022/3/17 8:56
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 登录校验
     *
     * @param phone         用户输入的手机号
     * @param code          用户输入的验证码
     * @param codeInSession 系统的验证码
     * @return
     */
    @Override
    public User login(String phone, String code, String codeInSession) {
        //1. 校验验证码是否为空并且两个验证码是否一致
        User dbUser = null;
        if (codeInSession != null && codeInSession.equalsIgnoreCase(code)) {
            //验证码没有问题
            //2. 根据手机号查询用户，
            dbUser = userMapper.findByPhone(phone);
            if (dbUser == null) {
                //3. 如果为空代表该用户是新用户，直接注册
                dbUser = new User();
                dbUser.setPhone(phone);
                dbUser.setStatus(1);
                userMapper.save(dbUser);
            }
        }
        System.out.println(dbUser);
        return dbUser;
    }
}
